package com.andong.controller;

import com.andong.annotations.AccessControl;
import com.andong.domain.dto.UpdatePasswordDTO;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.service.StudentService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDate;

import static com.andong.utils.SystemConstant.*;

/**
 * (Student)表控制层
 *
 * @author 王海云
 * @since 2023-12-30 19:29:53
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

    @GetMapping("/{id}")
    @AccessControl(roles = STUDENT)
    public Result selectById(@PathVariable Long id) {
        return studentService.selectById(id);
    }

    @PutMapping("/updatePassByCode")
    @AccessControl(roles = STUDENT)
    public Result updatePasswordByCode(@RequestBody UpdatePasswordDTO dto) throws Exception {
        return studentService.updatePasswordByCode(dto);
    }

    @PutMapping("/updatePassByPre")
    @AccessControl(roles = STUDENT)
    public Result updatePasswordByPre(@RequestBody UpdatePasswordDTO dto) throws Exception {
        return studentService.updatePasswordByPre(dto);
    }

    @GetMapping("/list")
    @AccessControl(roles = {COACH, ADMIN, SUPER_ADMIN}, permission = "学员管理")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender, Integer licenseType, Integer status, Integer learningStage,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageResult pageResult = studentService.queryList(page, pageSize, name, gender, licenseType, status, learningStage, begin, end);
        return Result.success(pageResult);
    }

    @PutMapping("/image")
    @AccessControl(roles = STUDENT)
    public Result updateImage(String newUrl) {
        studentService.updateImage(newUrl);
        return Result.success();
    }

    @PutMapping("/phone")
    @AccessControl(roles = STUDENT)
    public Result updatePhone(String phone, String code) {
        return studentService.updatePhone(phone, code);
    }

    @PutMapping("/status")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "学员管理")
    public Result updateStatus(Long id, Integer status) {
        return studentService.updateStatus(id, status);
    }

    @DeleteMapping("/{id}")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "学员管理")
    public Result delete(@PathVariable Long id) {
        return studentService.delete(id);
    }
}

